"""
@File    : action_method.py
@Author  : qve-wzy
@Time    : 2020/9/5 0005 7:55
"""
from  util.get_local_data import GetByLocal
from  base.base_dirver import  BaseDriver

import time
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

class ActionMethod:
    '''
    封装excel用例中各种操作动作
    '''
    def __init__(self):
        base_driver= BaseDriver()
        self.driver = base_driver.android_driver(0)
        self.get_by_local = GetByLocal(self.driver)



    def input(self, *args):
        '''
        输入值
        '''
        # key,value
        element = self.get_by_local.get_element(args[0])
        if element == None:
            return args[0], "元素没找到"
        element.send_keys(args[1])

    def on_click(self, *args):
        '''
        点击元素
        '''
        element = self.get_by_local.get_element(args[0])
        if element == None:
            return args[0], "元素没找到"
        element.click()

    def sleep_time(self, *args):
        '''
        等待
        '''
        time.sleep(int(args[0]))

    def get_size(self, *args):
        '''
        获取屏幕的宽高
        '''
        size = self.driver.get_window_size()
        width = size['width']
        height = size['height']
        return width, height

    def swipe_left(self, *args):
        '''
        向左边滑动
        '''
        x1 = self.get_size()[0] / 10 * 9
        y1 = self.get_size()[1] / 2
        x = self.get_size()[0] / 10
        self.driver.swipe(x1, y1, x, y1, 2000)

    def swipe_right(self, *args):
        '''
        向右边滑动
        '''
        x1 = self.get_size()[0] / 10
        y1 = self.get_size()[1] / 2
        x = self.get_size()[0] / 10 * 9
        self.driver.swipe(x1, y1, x, y1, 2000)

    def swipe_up(self, *args):
        '''
        向上滑动
        '''
        x1 = self.get_size()[0] / 2
        y1 = self.get_size()[1] / 10 * 6
        y = self.get_size()[1] / 10 * 2
        self.driver.swipe(x1, y1, x1, y, 1000)

    def swipe_down(self, *args):
        '''
        向下滑动
        '''
        x1 = self.get_size()[0] / 2
        y1 = self.get_size()[1] / 10
        y = self.get_size()[1] / 10 * 9
        self.driver.swipe(x1, y1, x1, y)

    def get_element(self, *args):
        '''
        获取元素
        '''
        element = self.get_by_local.get_element(args[0])
        if element == None:
            return None
        return element

    def get_tost_element(self, *args):
        '''
        获取提示框元素
        '''
        time.sleep(2)
        tost_element = ("xpath", "//*[contains(@text," + args[0] + ")]")
        return WebDriverWait(self.driver, 10, 0.1).until(EC.presence_of_element_located(tost_element))

if __name__ == '__main__':
    pass